<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\Db;
use think\Request;
use app\admin\validate\CheckOrder;
use think\exception\ValidateException;
class Order extends Base
{


    protected $mod;

    protected function initialize()
    {
        parent::initialize();
        $this->mod = new \app\admin\model\Order;
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($query=[],int $page=1,int $limit=50)
    {
        $map=[];
        if($query){
            foreach($query as $k=>$v){
                $value=$v['value'];
                $map[]=[$v['col'],'like',"%$value%"];
            }
        }
        $roles_data = [];
        foreach ($this->admin->roles as $role) {
            $roles_data[] = $role->id;
        }
        if(in_array(6,$roles_data)){
            $map[] = ['status','=',1];
        }
        if(in_array(5,$roles_data)){
            $map[] = ['admin_id','=',$this->request->uid];
        }
        $dataList = $this->mod::with('orderCompany')
            ->where($map)
            ->append(['status_text','admin_id_name'])
            ->order('status asc,update_time desc')
            ->paginate($limit, false, ['page' => $page, 'query' => $query]);
        return $this->success('ok', $dataList);
    }

    public function save()
    {
        $data = $this->request->param();
        $data['admin_id']=$this->admin->id;
        try {
            //验证
            $valCheck = validate(CheckOrder::class)->check($data);
            if ($valCheck !== true) {
               return $this->error($valCheck);
            }
        } catch (ValidateException $e) {
            return $this->error($e->getError());
        }
        $data['status'] = 1;
        $data['sn'] = date('YmdHis').substr(implode('', array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        if ($this->mod::create($data)) {
            return $this->success('增加成功');
        } else {
            return $this->error('增加失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $res=$this->mod::with('orderCompany')->append(['status_text'])->where('id',$id)->find();
        return $this->success('ok',$res);
    }


    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update( $id=0)
    {  
        $data = $this->request->param();
        if(empty($data['status'])){
            $data['status'] = 1;
            $data['refuse'] = '';
        }
        if($this->mod::update($data)){
            return $this->success('更新成功');
        }else{
            return $this->error('更新失败');
        }
    }


    public function delete($ids = [])
    {
        if (!$ids) {
            return 0;
        }
        if (is_array($ids)) {
            $ids = array_map('intval', $ids);
        }
        $rs = $this->mod::destroy($ids);
        if ($rs) {
           return $this->success("删除完毕");
        }
    }
}
